package com.ruoyi.web.course.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.web.course.domain.TrainVideo;
import com.ruoyi.web.course.service.ITrainVideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.web.course.mapper.TrainCourseChapterMapper;
import com.ruoyi.web.course.domain.TrainCourseChapter;
import com.ruoyi.web.course.service.ITrainCourseChapterService;

/**
 * 章节Service业务层处理
 *
 * @author 范书铭
 * @date 2024-10-09
 */
@Service
public class TrainCourseChapterServiceImpl extends ServiceImpl<TrainCourseChapterMapper, TrainCourseChapter> implements ITrainCourseChapterService {
    @Autowired
    private TrainCourseChapterMapper trainCourseChapterMapper;
    @Autowired
    private ITrainVideoService trainVideoService;
    /**
     * 查询章节列表
     *
     * @param trainCourseChapter 章节
     * @return 章节
     */
    @Override
    public List<TrainCourseChapter> selectTrainCourseChapterList(TrainCourseChapter trainCourseChapter) {
        List<TrainCourseChapter> trainCourseChapters
                = trainCourseChapterMapper.selectTrainCourseChapterList(trainCourseChapter);
        List<Long> videoIds = trainCourseChapters.stream().map(TrainCourseChapter::getVideoId).collect(Collectors.toList());
        List<TrainVideo> trainVideos = trainVideoService.list(new LambdaQueryWrapper<TrainVideo>()
                .in(TrainVideo::getId,videoIds));
        trainCourseChapters.forEach(trainCourseChapter1 -> {
            for(TrainVideo trainVideo : trainVideos){
                if(trainVideo.getId().longValue() ==trainCourseChapter1.getVideoId()){
                    trainCourseChapter1.setTrainVideo(trainVideo);
                }
            }
        });
        return trainCourseChapters;
    }
}